package com.supplychain.scm.core.delay.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.util.Assert;

import java.util.List;

/**
 * @author liyang
 * @version 1.0.0
 * @ClassName RedissonProperties
 * @createTime 17:17:00 2022/10/14
 */
@Data
@ConfigurationProperties(prefix = RedissonProperties.PREFIX)
public class RedissonProperties {

    //public static final String PREFIX = "dby.delay-redisson";
    public static final String PREFIX = "spring.redis";
    /**
     * Database index used by the connection factory.
     */
    private int database = 0;
    /**
     * Connection URL. Overrides host, port, and password. User is ignored. Example:
     * redis://user:password@example.com:6379
     */
    private String url;
    /**
     * Redis server host.
     */
    private String host = "localhost";
    /**
     * Login username of the redis server.
     */
    private String username;
    /**
     * Login password of the redis server.
     */
    private String password;
    /**
     * Redis server port.
     */
    private int port = 6379;
    /**
     * Whether to enable SSL support.
     */
    private boolean ssl;

    private Cluster cluster;

    /**
     * redisson配置
     */
    private Redisson delayRedisson;
    /**
     * Cluster properties.
     */
    @Data
    public static class Cluster {

        /**
         * Comma-separated list of "host:port" pairs to bootstrap from. This represents an
         * "initial" list of cluster nodes and is required to have at least one entry.
         */
        private List<String> nodes;

        /**
         * Maximum number of redirects to follow when executing commands across the
         * cluster.
         */
        private Integer maxRedirects;
    }

    @Data
    public static class Redisson{
        private boolean enabled = false;

        //private ServerType serverType = ServerType.SINGLE;

        //private String serverAddress = "redis://localhost:6379";

        private int threads = 16;

        private int nettyThreads = 32;

        private int lockWatchdogTimeoutMillis = 30000;

        private int pingTimeoutMillis = 1000;

        private int connectTimeoutMillis = 10000;

        private int socketTimeoutMillis = 3000;

        private boolean keepAlive;

        private int retryAttempts = 3;

        private int retryIntervalMillis = 1500;

        private int maxPoolSize = 64;

        private int minIdleSize = 24;

        private int maxIdleMillis = 10000;

        private MasterSlaveProperties master;

        private MasterSlaveProperties slave;

        /**
         * @param threads 为零表示cpu核数的2倍线程
         */
        public void setThreads(int threads) {
            Assert.isTrue(threads >= 0, "threads must be gte 0");
            this.threads = threads;
        }

        /**
         * @param nettyThreads 为零表示cpu核数的2倍线程
         */
        public void setNettyThreads(int nettyThreads) {
            Assert.isTrue(nettyThreads >= 0, "threads must be gte 0");
            this.nettyThreads = nettyThreads;
        }

        public void setLockWatchdogTimeoutMillis(int lockWatchdogTimeoutMillis) {
            Assert.isTrue(lockWatchdogTimeoutMillis > 0, "lockWatchdogTimeoutMillis must be gt 0");
            this.lockWatchdogTimeoutMillis = lockWatchdogTimeoutMillis;
        }


        public void setPingTimeoutMillis(int pingTimeoutMillis) {
            Assert.isTrue(pingTimeoutMillis > 0, "pingTimeoutMillis must be gt 0");
            this.pingTimeoutMillis = pingTimeoutMillis;
        }

        public void setConnectTimeoutMillis(int connectTimeoutMillis) {
            Assert.isTrue(connectTimeoutMillis > 0, "connectTimeoutMillis must be gt 0");
            this.connectTimeoutMillis = connectTimeoutMillis;
        }

        public void setSocketTimeoutMillis(int socketTimeoutMillis) {
            Assert.isTrue(socketTimeoutMillis > 0, "socketTimeoutMillis must be gt 0");
            this.socketTimeoutMillis = socketTimeoutMillis;
        }

        public void setRetryAttempts(int retryAttempts) {
            Assert.isTrue(retryAttempts >= 0, "retryAttempts must be gte 0");
            this.retryAttempts = retryAttempts;
        }

        public void setRetryIntervalMillis(int retryIntervalMillis) {
            Assert.isTrue(retryIntervalMillis > 0, "retryIntervalMillis must be gt 0");
            this.retryIntervalMillis = retryIntervalMillis;
        }

        public void setMaxPoolSize(int maxPoolSize) {
            Assert.isTrue(maxPoolSize > 0, "maxPoolSize must be gt 0");
            this.maxPoolSize = maxPoolSize;
        }

        public void setMinIdleSize(int minIdleSize) {
            Assert.isTrue(minIdleSize >= 0, "minIdleSize must be gte 0");
            this.minIdleSize = minIdleSize;
        }

        public void setMaxIdleMillis(int maxIdleMillis) {
            Assert.isTrue(maxIdleMillis >= 0, "maxIdleMillis must be gte 0");
            this.maxIdleMillis = maxIdleMillis;
        }
    }

    public void setDatabase(int database) {
        Assert.isTrue(database >= 0, "database must be gte 0");
        this.database = database;
    }


    @Data
    public static class MasterSlaveProperties {

        private int maxPoolSize = 64;

        private int minIdleSize = 24;

        public void setMaxPoolSize(int maxPoolSize) {
            Assert.isTrue(maxPoolSize > 0, "maxPoolSize must be gt 0");
            this.maxPoolSize = maxPoolSize;
        }

        public void setMinIdleSize(int minIdleSize) {
            Assert.isTrue(minIdleSize >= 0, "minIdleSize must be gte 0");
            this.minIdleSize = minIdleSize;
        }

    }

}
